import json

import requests
from config.cfg import *
from pprint import pprint


class Teacher:

    def _printResponse(self, response):
        print('\n\n-------- HTTP response * begin -------')
        print(response.status_code)

        for k, v in response.headers.items():
            print(f'{k}: {v}')

        print('')

        body = response.content.decode('utf8')
        print(body)

        try:
            jsonBody = response.json()
            print(f'\n\n---- 消息体json ----\n')
            pprint(jsonBody)
        except:
            print('消息体不是json格式！！')

        print('-------- HTTP response * end -------\n\n')

    # 列出老师
    def list_teacher(self, subjectid=None):
        params = {
            "vcode": g_code,
            "action": "search_with_pagenation",
        }

        if subjectid:
            params['subjectid'] = subjectid

        res = requests.get(url=g_api_url_teacher, params=params)

        self._printResponse(res)

        return res

    # 添加老师
    def add_teacher(self, username, realname, subjectid,
                    classlist, phonenumber, email, idcardnumber):

        # 处理参数classlist - 数据要求是json格式的字符串 - 注意这里的传入和处理都很妙
        classid_list = classlist.split(',')
        classlist2 = [{"id":int(cid.strip())} for cid in classid_list]

        data = {
            "vcode": g_code,
            "action": "add",
            "username": username,
            "realname": realname,
            "subjectid": subjectid,
            "classlist": json.dumps(classlist2),
            "phonenumber": phonenumber,
            "email": email,
            "idcardnumber": idcardnumber
        }

        res = requests.post(url=g_api_url_teacher, data=data)

        self._printResponse(res)

        return res

    # 修改老师
    def modify_teacher(self, teacherid, realname=None, subjectid=None, classlist=None,
                       phonenumber=None, email=None, idcardnumber=None):
        url = f"{g_api_url_teacher}/{teacherid}"
        data = {
            "vcode": g_code,
            "action": "modify"
        }

        if realname:
            data['realname'] = realname
        if subjectid:
            data['subjectid'] = subjectid
        if classlist:
            # 处理参数classlist - 数据要求是json格式的字符串 - 注意这里的传入和处理都很妙
            classid_list = classlist.split(',')
            classlist2 = [{"id": int(cid.strip())} for cid in classid_list]
            data['classlist'] = json.dumps(classlist2)
        if phonenumber:
            data['phonenumber'] = phonenumber
        if email:
            data['email'] = email
        if idcardnumber:
            data['idcardnumber'] = idcardnumber

        res = requests.put(url=url, data=data)

        self._printResponse(res)

        return res

    # 删除老师
    def delete_teacher(self, teacherid):
        url = f'{g_api_url_teacher}/{teacherid}'
        data = {
            "vcode": g_code
        }

        res = requests.delete(url=url, data=data)

        self._printResponse(res)

        return res

    # 删除所有老师
    def delete_all_teacher(self):
        ret = self.list_teacher()
        retList = ret.json()
        for one in retList['retlist']:
            self.delete_teacher(teacherid=one['id'])


tm = Teacher()
